import useDictStore from '@/store/modules/dict'
import { useDict } from '@/utils/dict'

/**
import { initDict, dictToName } from '@/utils/dictToName.js'
initDict(['room_use_type', 'building_sex_type'])

{{ dictToName('building_sex_type', form.sexType) }}
 */
/**
 * 将字典数据初始化到vuex中
 */
export function initDict(dicts) {
  if (Array.isArray(dicts)) {
    for (let item of dicts) {
      useDict(item)
    }
  } else {
    useDict(dicts)
  }
}

/**
 * 字典值翻译
 */
export function dictToName(dictType, value) {
  let dict = useDictStore().getDict(dictType)
  if (!dict) {
    useDict(dictType)
    return ''
  }

  const ss = dict.find((item) => {
    return item.value == value
  })
  return ss && ss.label
}
